<!--

-->


<template>
    <div class="Index">
        <!-- 筛选区 -->
        <table-search-bar :config="requestBodyConfig" :model="requestBody" title="审计日志" @search="searchClicked" />
        <!-- 内容 -->
        <div class="container">
            <vxe-table ref="VXETable" :column-config="{ resizable: false}" :data="state.list" :height="tableHeight" :loading="state.isLoading" :radio-config="{trigger: 'row', highlight: true}" :row-config="{isCurrent: true, isHover: true, keyField: 'id'}" :tree-config="{childrenField: 'children', reserve: true}" border round show-overflow size="small" @current-change="() => {}" @radio-change="() => {}" >

                <template #empty>
                    <el-empty :image-size="100" />
                </template>
                <vxe-column title="#" type="seq" width="50" />
                <vxe-column field="createdTime" title="操作时间" width="180">
                    <template #default="{ row }">
                        <span>{{ filters.dateFilter(row.createdTime) }}</span>
                    </template>
                </vxe-column>
                <vxe-column align="left" field="tableName" min-width="200" title="表名"></vxe-column>
                <vxe-column field="columnName" title="列名" width="200"></vxe-column>
                <vxe-column field="operate" title="操作方式" width="120"></vxe-column>
                <vxe-column field="newValue" min-width="200" title="旧值->新值">
                    <template #default="{ row }">
                        <span>{{ row.oldValue }} -> {{ row.newValue }}</span>
                    </template>
                </vxe-column>
                <vxe-column field="userName" title="操作人" width="120"></vxe-column>
            </vxe-table>
            <div class="page-config">
                <vxe-pager v-model:current-page="requestParams.pageIndex" v-model:page-size="requestParams.pageSize" :layouts="['PrevJump', 'PrevPage', 'JumpNumber', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']" :page-sizes="pageConfig.sizes" :total="pageConfig.total" background size="mini" @page-change="onPageChange" />
            </div>
        </div>
    </div>
</template>
<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import * as api from "./api.js"

const props = defineProps({});
const emits = defineEmits();

// 路由
const router = useRouter();
// store
const store = useStore();
const tableHeight = computed(() => store.getters.layoutType === "mobile" ? "" : store.getters.containerHeight - 194)

const {$message, $dayjs} = window;
// 当前页面权限
const permission = {
    add: "",
    edit: "",
    delete: "SysAuditLog:ClearAuditLog"
};

// 表单
const editForm = ref();

// 响应式数据
const state = reactive({
    list: [],
    isLoading: false
});
// 过滤器
const filters = computed(() => {
    return {
        dateFilter(val) {
            return $dayjs(val).format("YYYY-MM-DD HH:mm:ss")
        }
    }
})

// 请求query参数
const requestParams = reactive({
    orderCol: "id",
    isDesc: true,
    pageIndex: 1,
    pageSize: 20,
});
// 请求body参数
const requestBody = reactive({
    tableName: null,
    columnName: null,
    userName: null,
    operate: null
})
// 筛选区域配置
const requestBodyConfig = reactive({
    tableName: {label: "表名", component: "el-input"},
    columnName: {label: "列名", component: "el-input", inMore: true},
    userName: {label: "操作人", component: "el-input", inMore: true},
    operate: {label: "操作方式", component: "el-input", inMore: true},
})
// 分页配置
const pageConfig = reactive({
    total: 0,
    sizes: [10, 20, 50, 100, 200]
})

// 点击搜索
function searchClicked() {
    console.log("搜索条件: ", requestBody)
    reload(true);
}

// 查询列表
async function loadList() {
    const params = {
        ...requestParams
    };
    const body = {
        ...requestBody
    };
    state.isLoading = true;
    const {success, data} = await api.GetAuditLogPageList(params, body);
    state.isLoading = false;
    if (!success) return 0;
    const {items, totalCount} = data;
    state.list = items;
    pageConfig.total = totalCount;
}

// 添加
function addData() {
    // console.log(editForm)
    editForm.value.show({
        title: "添加",
        ok: () => {
            reload();
        }
    })
}

// 修改
function editData(row) {
    editForm.value.show({
        title: "修改",
        options: {...row},
        ok: () => {
            reload();
        }
    })
}

// 删除
async function deleteData({id}) {
    console.log("删除: ", id)
    const {success, data} = await api.ClearAuditLog({id});
    if (!success) return 0;
    reload();
    $message.success("删除成功")
}

// 分页事件
function onPageChange({type}) {
    if (type === "current") {
        loadList()
    }
    if (type === "size") {
        reload(true)
    }
}

// 刷新
function reload(re) {
    if (re) requestParams.pageIndex = 1;
    loadList()
}

onMounted(() => {
    loadList()
})
</script>

<style lang="scss" scoped>
.Index {
    margin: 10px;
    width: calc(100% - 20px);
    box-sizing: border-box;

    .container {
        padding: 10px;
        background: #fff;
        margin-top: 10px;
        box-shadow: 0 0 10px -2px rgba(0, 0, 0, 0.1);

    }
}
</style>
